
#include "wds_fade_blood.h"

WDSFadeBlood::WDSFadeBlood(): m_DecaySpeed(2.0f),
							 m_State(FADEBLOOD_STATE_NONE),
							 m_Alpha(0),
							 m_Duration(0)
{
	
}


bool	WDSFadeBlood::IsInsideScreen(const math::Vec2& Pos) const
{
	//const math::Vec2 HalfSize = m_Size * 0.5f;
	//return math::Intersection::AABB2D( m_Pos - HalfSize, m_Pos + HalfSize , math::Vec2(0,0) , math::Vec2(1.0f,1.0f) ); 
	return true;
}

void	WDSFadeBlood::Render(ui::UISprite& Sprite, const math::Vec2& Offset,float Scale)
{
	

	if(m_Alpha <= 0)
		return;

	if(m_State == FADEBLOOD_STATE_FINISH)
		return;

	//check isinside
	if(IsInsideScreen(m_Pos) == false)
		return;

	Sprite.m_Pos =  m_Pos - Offset;
		//m_Size.y = pInfo->m_Dist;
	Sprite.m_Size = Sprite.m_SizeOriginal * Scale;
	Sprite.m_Rotation = m_Rotation;
	Sprite.m_Color = ColorfRGBA(1.0f,0.0f,0.0f, m_Alpha);
	Sprite.RenderRotated();
	

}
	
void	WDSFadeBlood::RenderDebug(const math::Vec2& Offset,float Scale)
{
	
}
	


void	WDSFadeBlood::Tick(float dt)
{
	switch(m_State)
	{
		case FADEBLOOD_STATE_NONE:
			m_Alpha = 0;
		break;

		case FADEBLOOD_STATE_ALPHAUP:
			m_Alpha  += (dt);
			if(m_Alpha >= 1.0f)
			{
				m_Alpha = 1.0f;
				m_State = FADEBLOOD_STATE_STILL;
				m_Duration = 5.0f;
			}
		break;

		case FADEBLOOD_STATE_STILL:
			m_Alpha = 1.0f;
			m_Duration -= dt;
			if(m_Duration <= 0)
			{
				m_State = FADEBLOOD_STATE_ALPHADOWN;
			}
		break;

		case FADEBLOOD_STATE_ALPHADOWN:
			m_Alpha  -= ( dt * 0.2f);
			if(m_Alpha <= 0.0f)
			{
				m_State = FADEBLOOD_STATE_FINISH;
				m_Alpha = 0.0f;
			}
		break;

	}
}
	


	
void	WDSFadeBlood::ReSpawn(const math::Vec2& Pos, float Rotation)
{
	m_State = FADEBLOOD_STATE_ALPHAUP;
	m_Pos = Pos;
	m_Rotation = Rotation;
}

//------------------------------------------------------------------------------------

WDSFadeBloodManager&		WDSFadeBloodManager::Instance()
{
	static WDSFadeBloodManager Inst;
	return Inst;
}

WDSFadeBloodManager::WDSFadeBloodManager()
{
	m_Sprite.Create("bloodspot", math::Vec2(0,0), math::Vec2(1.0f,1.0f),false);
	m_Sprite.m_Size *= 0.5f;
}
	
WDSFadeBloodManager::~WDSFadeBloodManager()
{
	Clear();
}

void	WDSFadeBloodManager::Render(const math::Vec2& Offset,float Scale)
{
	for(int i = 0; i < (int)m_FadeBloods.size(); i++)
	{
		m_FadeBloods[i]->Render(m_Sprite, Offset,Scale);
	}
}
	
void	WDSFadeBloodManager::RenderDebug(const math::Vec2& Offset,float Scale)
{

}
	
void	WDSFadeBloodManager::Tick(float dt)
{
	for(int i = 0; i < (int)m_FadeBloods.size(); i++)
	{
		m_FadeBloods[i]->Tick(dt);
	}
}

void	WDSFadeBloodManager::Spawn(const math::Vec2& Pos, 
								   float Rotation,
								   float Duration)
{
	bool	bSpawn =false;		
	
	WDSFadeBlood* pBlood = NULL;
	
	bSpawn =false;
	for( int i = 0; i < (int)m_FadeBloods.size(); i++)
	{
		pBlood = m_FadeBloods[i];
		if(pBlood->IsReadyToSpawn())
		{
			
			bSpawn = true;
			i = (int)m_FadeBloods.size();
		}
	}

	if(!bSpawn)
	{
		
		pBlood = new WDSFadeBlood();
		m_FadeBloods.push_back(pBlood);
			
	}

	if(pBlood)
	{	
		pBlood->ReSpawn(Pos,Rotation);
	}
}

void	WDSFadeBloodManager::Clear()
{
	for(int i = 0; i < (int)m_FadeBloods.size(); i++)
	{
		UTIL_SAFE_DELETE(m_FadeBloods[i]);
	}
	m_FadeBloods.resize(0);
}

